package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.Database;

public class Calendar {

	/*
	 * REGION private fields
	 */
	private Person owner;
	private ArrayList<Appointment> appointments;
	
	/*
	 * REGION public constructor
	 */
	public Calendar(Person owner) {
		setOwner(owner);
		
		appointments = new ArrayList<Appointment>();
		
		loadPersonalCalendar();
	}
	
	/*
	 * REGION public getters
	 */
	public Person getOwner() {
		return owner;
	}
	
	public ArrayList<Appointment> getAppointments() {
		return this.appointments;
	}
	
	/*
	 * REGION public setters
	 */
	public void setOwner(Person owner) {
		this.owner = owner;
	}
	
	/*
	 * REGION public methods
	 */
	public void loadPersonalCalendar() {
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getUserAppointment(owner.getID());
			
			while (rs.next()) {
				int appointmentID = rs.getInt("ID");	
				Appointment appointment = new Appointment(appointmentID);
				appointments.add(appointment);
			}
			
			rs = database.getUserMeeting(owner.getID());
			
			while (rs.next()) {
				int meetingID = rs.getInt("meetingID");
				
				Meeting meeting = new Meeting(meetingID);
				if (!appointments.contains(meeting)) {
					appointments.add(meeting);
				}
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
}
